package com.check.controller;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.PageObject;
import com.check.pojo.Pcwh;
import com.check.service.PcwhService;

@Controller
public class PcwhController  extends BaseController{

	@Autowired
	@Qualifier("pcwhService")
	private PcwhService  pcwhService;
	
	@RequestMapping("pcwhQuery.do")
	public ModelAndView pcwhQuery(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		if(pageObject.getCurrent()==0){
			pageObject.setCurrent(1);
		}
		List<Pcwh> pcwhList=pcwhService.findAllPcwh(pageObject);
		saveSysLog(RE_KHPC, LOG_QUERY, request);
		pageObject.setUrl("pcwhQuery.do");
		pageObject.setTotal(pcwhService.findPcwhCount());
		return new ModelAndView("page/pcwh/pcwhList")
					.addObject("path",request.getContextPath())
					.addObject("pcwhList",pcwhList)
					.addObject("page",pageObject.toPage());
	}
	
	@RequestMapping("toAddPcwh.do")
	public ModelAndView toAddPcwh(HttpServletRequest request,HttpServletResponse response){
		
		return new ModelAndView("page/pcwh/pcwhAdd")
					.addObject("path",request.getContextPath());
	}
	
	@RequestMapping("addPcwh.do")
	public void addPcwh(HttpServletRequest request,HttpServletResponse response,
				@ModelAttribute("pcwh")Pcwh pcwh){
		PrintWriter out=null;
		String flag="0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			String groupMemo = request.getParameter("groupMemo");
			pcwh.setGroupMemo(URLDecoder.decode(groupMemo, "UTF-8"));
			saveSysLog(RE_KHPC, LOG_ADD, request);
			if(pcwhService.insertPcwh(pcwh)){
				flag="1";
			}
		}catch (Exception e) {
			e.printStackTrace();
			flag="0";
		}finally{
			out.print(flag);
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}
	
	@RequestMapping("findPcwh.do")
	public void findPcwh(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String checkYear=request.getParameter("checkYear");
		String flag = "0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			Pcwh findPcwh = pcwhService.findByCheckYear(checkYear);
			if(findPcwh!=null){
				flag="1";
			}else{
				flag="0";
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping("toUpdatePcwh.do")
	public ModelAndView toUpdatePcwh(HttpServletRequest request,HttpServletResponse response){
		String checkYear = request.getParameter("checkYear");
		Pcwh pcwh = pcwhService.findByCheckYear(checkYear);
		return new ModelAndView("page/pcwh/pcwhUpdate")
					.addObject("path",request.getContextPath())
					.addObject("pcwh",pcwh);
	}
	
	@RequestMapping("updatePcwh.do")
	public void updatePcwh(HttpServletRequest request,HttpServletResponse response,
				@ModelAttribute("pcwh")Pcwh pcwh){
		PrintWriter out=null;
		String flag="0";
		try{
			response.setContentType("text/html;charset=gbk");
			String groupMemo = request.getParameter("groupMemo");
			pcwh.setGroupMemo(URLDecoder.decode(groupMemo, "UTF-8"));
			out=response.getWriter();
			saveSysLog(RE_KHPC, LOG_UPDATE, request);
			if(pcwhService.updatePcwh(pcwh)){
				flag="1";
			}
		}catch (Exception e) {
			e.printStackTrace();
			flag="0";
		}finally{
			out.print(flag);
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}
	
	
	@RequestMapping("deletePcwh.do")
	public void deletePcwh(HttpServletRequest request,HttpServletResponse response,
				@ModelAttribute("pcwh")Pcwh pcwh){
		PrintWriter out=null;
		String flag="0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			String checkYear = request.getParameter("checkYear");
			saveSysLog(RE_KHPC, LOG_DELETE, request);
			if(pcwhService.deletePcwh(checkYear)){
				flag="1";
			}
		}catch (Exception e) {
			e.printStackTrace();
			flag="0";
		}finally{
			out.print(flag);
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}
	
	@RequestMapping("activePcwh.do")
	public void activePcwh(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String flag="0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			String checkYear = request.getParameter("checkYear");
			saveSysLog(RE_KHPC, LOG_ACTIVE, request);
			if(pcwhService.updateActivePcwh(checkYear)){
				flag="1";
			}
		}catch (Exception e) {
			e.printStackTrace();
			flag="0";
		}finally{
			out.print(flag);
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}
	
	@RequestMapping("unactivePcwh.do")
	public void unactivePcwh(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String flag="0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			String checkYear = request.getParameter("checkYear");
			saveSysLog(RE_KHPC, LOG_UNACTIVE, request);
			if(pcwhService.updateUnactivePcwh(checkYear)){
				flag="1";
			}
		}catch (Exception e) {
			e.printStackTrace();
			flag="0";
		}finally{
			out.print(flag);
			if(out!=null){
				out.flush();
				out.close();
			}
		}
	}
}
